package com.lmm.lcbusiness.lcbusiness.diyviews;

import android.content.Context;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.SearchView;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.RelativeLayout;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.lmm.dresswisdom.lmmframe.util.ToastUtils;
import com.lmm.lcbusiness.lcbusiness.R;


/**
 * Created by xmgong on 2017/4/25.
 * 仿电话本控件
 */

public class PhoneRecycleView extends RelativeLayout implements SideBar.OnTouchingLetterChangedListener {

    SideBar mSideBar;
    RecyclerView mRecyclerView;
    View noResultView;
    private SearchView mSearchView;
    BaseQuickAdapter mBaseQuickAdapter;
    SideBar.OnTouchingLetterChangedListener mOnTouchingLetterChangedListener;


    public void setOnTouchingLetterChangedListener(SideBar.OnTouchingLetterChangedListener onTouchingLetterChangedListener) {
        mOnTouchingLetterChangedListener = onTouchingLetterChangedListener;
        mSideBar.setOnTouchingLetterChangedListener(mOnTouchingLetterChangedListener);
    }


    //设置数据源
    public void setBaseQuickAdapter(BaseQuickAdapter baseQuickAdapter) {
        mBaseQuickAdapter = baseQuickAdapter;
        mRecyclerView.setLayoutManager(new LinearLayoutManager(getContext()));
        mRecyclerView.setAdapter(mBaseQuickAdapter);
    }

    public PhoneRecycleView(Context context) {
        super(context);
        initView();
    }

    public PhoneRecycleView(Context context, AttributeSet attrs) {
        super(context, attrs);
        initView();
    }

    public PhoneRecycleView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initView();
    }

    private void initView() {
        LayoutInflater.from(getContext()).inflate(R.layout.view_phonerecy, this, true);
        mSideBar = (SideBar) findViewById(R.id.sidebar);
        mRecyclerView = (RecyclerView) findViewById(R.id.recy);
        mSideBar.setOnTouchingLetterChangedListener(this);
    }


    public void moveToItem(int position) {
        if (mBaseQuickAdapter.getHeaderLayoutCount() > 0) {
            ((LinearLayoutManager) mRecyclerView.getLayoutManager()).scrollToPositionWithOffset(position + 1, 0);
        } else {
            ((LinearLayoutManager) mRecyclerView.getLayoutManager()).scrollToPositionWithOffset(position, 0);
        }
    }

    //添加搜索没有结果页面
    public void setNoResultView(View noResultView) {
        this.noResultView = noResultView;
        if (mBaseQuickAdapter == null) {
            ToastUtils.showToastShort("请先设置 BaseQuickAdapter");
        } else {
            mBaseQuickAdapter.addHeaderView(noResultView);
        }
    }

    public void showNoResultView() {
        noResultView.setVisibility(VISIBLE);
    }

    public void hideNoResultView() {
        noResultView.setVisibility(GONE);
    }

    //添加搜索
    public void addSearchBar() {
        if (mBaseQuickAdapter == null) {
            ToastUtils.showToastShort("请先设置 BaseQuickAdapter");
        } else {
            mSearchView = new SearchView(getContext());
            mSearchView.setIconifiedByDefault(false);
            mBaseQuickAdapter.addHeaderView(mSearchView);
        }
    }

    public SearchView getSearchView() {
        return mSearchView;
    }

    @Override
    public void onTouchingLetterChanged(String s) {

    }
}
